import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * The type Menu frame.
 *
 * @author Sihan Wang
 * @date 24/06/09
 */
public class MenuFrame extends JFrame implements ActionListener {
    /**
     * The Instruction.
     */
    JButton instruction = new JButton("Instruction");
    /**
     * The Recipe recommendation.
     */
    JButton recipeRecommendation = new JButton("<html>Recipe<br>recommendation</html>");
    /**
     * The Search food.
     */
    JButton searchFood = new JButton("Find Recipe");
    /**
     * The Refrigerator.
     */
    JButton refrigerator = new JButton("Refrigerator");
    /**
     * The Add recipe.
     */
    JButton addRecipe = new JButton("Add recipe");
    /**
     * The Back.
     */
    JButton back = new JButton("Back");
    /**
     * The Button panel.
     */
    JPanel buttonPanel = new JPanel();
    /**
     * The Panel.
     */
    JPanel panel = new JPanel();
    /**
     * The Background label.
     */
    JLabel backgroundLabel = new JLabel();
    /**
     * The Dialog label.
     */
    JLabel dialogLabel = new JLabel();
    /**
     * The J dialog.
     */
    JDialog jDialog = new JDialog();

    /**
     * Instantiates a new Menu frame.
     */
    public MenuFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                menuInterface();
                menuElement();
            }
        });
        this.setVisible(true);
    }

    /**
     * Menu interface.
     */
    public void menuInterface() {
        this.setTitle("Menu page");
        this.pack();
        this.setSize(new Dimension(1200, 675));
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());

        Path filePath = Paths.get(System.getProperty("user.dir"), "resources/background.png");
        String backgroundFilePath = filePath.toString();

        backgroundLabel = new JLabel(new ImageIcon(backgroundFilePath));
        backgroundLabel.setBounds(0, 0, 1200, 675);
        backgroundLabel.setLayout(new BorderLayout());
        backgroundLabel.add(buttonPanel);
        backgroundLabel.setVisible(true);
        panel.add(backgroundLabel);
        this.getContentPane().add(panel);
    }

    /**
     * Menu element.
     */
    public void menuElement() {
        instruction.setBounds(50, 50, 150, 100);
        instruction.setFont(new Font("Posterama", Font.PLAIN, 20));
        instruction.setBackground(Color.orange);
        instruction.addActionListener(this);

        recipeRecommendation.setBounds(250, 330, 250, 100);
        recipeRecommendation.setFont(new Font("Posterama", Font.PLAIN, 20));
        recipeRecommendation.setBackground(Color.orange);
        recipeRecommendation.addActionListener(this);

        searchFood.setBounds(630, 330, 250, 100);
        searchFood.setFont(new Font("Posterama", Font.PLAIN, 20));
        searchFood.setBackground(Color.orange);
        searchFood.addActionListener(this);

        refrigerator.setBounds(250, 500, 250, 100);
        refrigerator.setFont(new Font("Posterama", Font.PLAIN, 20));
        refrigerator.setBackground(Color.orange);
        refrigerator.addActionListener(this);

        addRecipe.setBounds(630, 500, 250, 100);
        addRecipe.setFont(new Font("Posterama", Font.PLAIN, 20));
        addRecipe.setBackground(Color.orange);
        addRecipe.addActionListener(this);

        buttonPanel.setLayout(null);
        buttonPanel.setOpaque(false);
        buttonPanel.add(instruction);
        buttonPanel.add(recipeRecommendation);
        buttonPanel.add(searchFood);
        buttonPanel.add(refrigerator);
        buttonPanel.add(addRecipe);
    }

    /**
     * Gets dialog.
     */
    public void getDialog() {
        JPanel dialogPanel = new JPanel();
        jDialog.pack();
        jDialog.setSize(new Dimension(1200, 675));

        Path filePath = Paths.get(System.getProperty("user.dir"), "resources/basicBackground.png");
        String basicBackgroundFilePath = filePath.toString();

        dialogLabel = new JLabel(new ImageIcon(basicBackgroundFilePath));
        dialogLabel.setBounds(0, 0, 1200, 675);
        dialogLabel.setLayout(null);
        dialogLabel.setVisible(true);
        dialogPanel.add(dialogLabel);

        back.setBounds(20, 20, 80, 80);
        back.setFont(new Font("Posterama", Font.PLAIN, 20));
        back.setBackground(Color.orange);
        back.addActionListener(this);
        dialogLabel.add(back);

        jDialog.setAlwaysOnTop(true);
        jDialog.setLocationRelativeTo(null);

        String textOfInstruction = """
                                                                                             Instruction
                                                                                   
                1.This system is designed as a smart refrigerator which can provide options like new food ingredient records, new recipe records, existing food ingredient records in your refrigerator, search for recipe according to the title entered by you and recipe recommendation based on food ingredient you have in the refrigerator.
                2.Usage steps:\s
                Open the system, you will see the menu interface. There are five buttons including instruction, recipe recommendation, search food, refrigerator and add recipe.
                 (1)Click the ‘recipe recommendation’ button, enter an automatic recipe recommendation interface. The system will recommend three recipes based on your existing food ingredients. Also, there is a ‘back’ button in the upper left corner of the interface.
                  A.Click the ‘cook’ button at the bottom of the recite if there are any dishes you want to eat, then the system will confirm with you whether to make this dish and the ingredients to be consumed. You will return to menu interface automatedly after confirming your choice.
                  B.If there are no dishes you like, you can click the ‘back’ button to return to menu interface.
                  C.You can click the ‘back’ button to return to the menu interface even when there is no confirmation dialog window.
                 (2)Click the ‘search food’ button
                  A.There is a search box on the top of interface. Also, there is a ‘back’ button in the upper left corner of the interface.
                  B.Enter a dish you want to eat
                  C.The system will recommend the recipe of dish based on the dish title you input.
                  D.You can click the ‘cook’ button at the bottom if you want to eat. System will check what ingredients you have and provide corresponding reminders.\s
                  E.If this dish you are able to do, click the ‘cook’ button at the bottom of the recipe, the system will confirm with you whether to make this dish and the ingredients to be consumed. You will return to menu interface automatically after confirming your choice.
                  F.If there are no dish you like or you want to exit, you can click the ‘back’ button to return to the menu interface.
                 (3)Click the ‘refrigerator’ button
                  A.There are a refrigerator and a basket. Also, there is a ‘back’ button in the upper left corner of the interface.
                  B.The refrigerator displays the names and amounts of ingredients you have.
                  C.Click the bottom under the basket and you can clean all food in the refrigerator.
                  D.Click the basket and you can add new ingredients into the refrigerator.
                  E.When editing the ingredients, there will be a pop-up window which require you to enter the name, amount and unit of this ingredients. The unit includes milliliters, gram and pcs.
                  F.Click 'add' button on the pop-up window, the addition situation will be displayed in the blank space at the bottom of the page.
                  G.Click 'remove' button on the pop-up window, the system will check if there are enough ingredients to remove and show the removal situation in the blank space at the bottom of the page.
                  H.Enter food information until you do not have ingredients to add, you can click the ‘end’ button on the pop-up window to return to the refrigerator and basket page.
                  I.You can click the ‘back’ button on the refrigerator page to return to the menu interface.
                 (4)Click the ‘add recipe’ button
                  A.First, enter the title of the dish.
                  B.Second, input the ingredients you need in the recipe you want to add. These steps are the same as adding ingredients on the refrigerator page.
                  C.Enter details of recite (i.e., how to use these ingredients and how to cook).
                  D.Click the ‘confirm’ button to end the input, save the recipe and return to menu interface.
                 (5)At any time, you can click on the cross in the upper right corner window to end running the entire program.              
                """;
        JTextArea textArea = new JTextArea();
        textArea.setText(textOfInstruction);
        textArea.setFont(new Font("Posterama", Font.PLAIN, 20));
        textArea.setOpaque(false);
        textArea.setForeground(Color.BLACK);
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setCaretPosition(0);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(textArea);
        scrollPane.setBounds(100, 100, 1050, 480);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
        verticalScrollBar.setValue(0);

        dialogLabel.add(scrollPane);

        jDialog.setLayout(new BorderLayout());
        jDialog.getContentPane().add(dialogPanel);
        jDialog.setModal(true);
        jDialog.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        Object obj = e.getSource();

        if (obj == instruction) {
            this.setVisible(false);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    getDialog();
                }
            });
        }
        if (obj == recipeRecommendation) {
            this.setVisible(false);
            new RecipeRecommendationFrame();
        }
        if (obj == searchFood) {
            this.setVisible(false);
            new SearchRecipeFrame();
        }
        if (obj == refrigerator) {
            this.setVisible(false);
            new RefrigeratorFrame();
        }
        if (obj == addRecipe) {
            this.setVisible(false);
            new AddRecipeName();
        }
        if (obj == back) {
            jDialog.setVisible(false);
            new MenuFrame();
        }
    }
}
